-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Add transaction management helper #314
base: main
Are you sure you want to change the base?
Conversation
Hi @stephenafamo, Could you please review this proposal? |
My first instinct is that this is not useful to Bob. While someone may decide to use a transaction wrapper, there is no need to bake this into Bob. The reason is that every Bob method take a
You can then use this In your changes, I can rewrite your func testOperationsWithDatabase(ctx context.Context, db *sql.DB, value int32) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel() // rollback transaction if not committed
sqlTx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
tx := bob.NewTx(sqlTx)
q := psql.Update(
um.Table("table"),
um.Set(psql.Quote("field").EQ(psql.Arg(value))),
)
_, err = q.Exec(ctx, tx)
if err != nil {
return err
}
q2 := psql.Insert(
im.Into("table2"),
im.Values(psql.Quote("value").EQ(psql.Arg(value))),
)
_, err = q2.Exec(ctx, tx)
if err != nil {
return err
}
return tx.Commit()
} |
@stephenafamo thank you for the quick response. I understand your opinion. I would like to have such a helper as part of |
My bad 🤦🏾 . I missed calling
🤔 Alright, I see how this could be useful. func (db bob.DB) InTx(ctx context.Context, fn TxFunc, opts ...TxOption) error {
// ...
} Would this work well for your usecase? |
I see the only benefit of a dedicated package is the ability to register additional callbacks like |
c8e2db2
to
2f9063b
Compare
2f9063b
to
ae120dd
Compare
@stephenafamo I updated PR, but I was unable to test these changes within my code, since the newest version has some breaking changes. After I regenerated the code I see many errors related to missing method |
Yes, there were a lot of changes in Unfortunately, the docs are not updated yet 😔, also take a look at #310 for some details and examples. |
Thank you very much for the example. However, this has come as an unpleasant surprise. It will necessitate changes in several services on our side if we want to upgrade the version. |
I apologise for the breaking changes, it was not made lightly. One of the main driving factors was making hooks robust. Unfortunately, it was very easy to write/use queries that bypassed hooks and I really want Bob to have very few footguns. If something can be done, it should work as expected. |
Hello @dmakushin 👋🏾 Any progress with this? |
Suggestion is to add helper method similar to the one described at
https://bun.uptrace.dev/guide/transactions.html#runintx